import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:gt_flutter_admin/constant/error_msg.dart';
import 'package:gt_flutter_admin/pages/system/menu/menu_update_contrller.dart';
import 'package:gt_flutter_admin/enum/menu_type.dart';
import 'package:gt_flutter_admin/utils/data_format_utils.dart';

class MenuUpdateView extends GetView<MenuUpdateController> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(15),
      child: ListView(children: [_buildForm()]),
    );
  }

  // 表单
  Form _buildForm() {
    return Form(
      child: Column(
        children: [
          FormField(
            builder: (field) => BrnTextInputFormItem(
              controller: controller.menuNameText,
              title: '菜单名称',
              isRequire: true,
              hint: '请输入菜单名称',
              error: field.errorText ?? '',
            ),
            validator: (value) =>
                ErrorMsgs.validNull(controller.menuNameText.text),
          ),
          Obx(
            () => BrnTextSelectFormItem(
              title: '父菜单',
              hint: '请选择菜单名称',
              value: controller.parentName.value,
              onTap: () {
                controller.parentName.value = '';
                controller.parentId = null;

                BrnPickerEntity topMenu = DataFormatUtils.brnPickerEntityMap(
                  controller.menuPage.value.rows!,
                  PickerFilterType.radio,
                );

                _buildRangePicker(topMenu);
              },
            ),
          ),
          BrnTextInputFormItem(
            controller: controller.pathText,
            title: '对应路由',
            hint: '请输入路由',
          ),
          BrnTextInputFormItem(
            controller: controller.permsText,
            title: '权限码',
            hint: '请输入权限码',
          ),
          BrnTextInputFormItem(
            controller: controller.iconText,
            title: '图标',
            hint: '请输入图标',
          ),
          FormField(
            builder: (field) => BrnRadioInputFormItem(
              title: '类型',
              isRequire: true,
              options: [
                for (int i = 0; i < MenuType.values.length; i++)
                  MenuType.values[i].value,
              ],
              value: controller.type.value,
              error: field.errorText ?? '',
              onChanged: (oldStr, newStr) =>
                  controller.type.value = newStr ?? '',
            ),
            validator: (value) =>
                ErrorMsgs.validNull(controller.menuNameText.text),
          ),
          BrnTextInputFormItem(
            controller: controller.orderNumText,
            title: '排序',
            hint: '请输入序号',
          ),
          Builder(
            builder: (context) {
              return BrnBigMainButton(
                title: '提交',
                onTap: () {
                  if (Form.of(context).validate()) {
                    controller.onSubmit();
                  }
                },
              );
            },
          ),
        ],
      ),
    );
  }

  // 构建级联选择
  void _buildRangePicker(BrnPickerEntity topMenu) {
    showModalBottomSheet(
      context: Get.context!,
      backgroundColor: Colors.transparent,
      builder: (BuildContext dialogContext) => BrnMultiColumnPicker(
        entity: topMenu,
        onEntityTap: (columnIndex, rowIndex, entity) {
          if (!controller.parentName.value.contains(entity.name)) {
            controller.parentId = int.parse(entity.key!);
            controller.parentName.value = entity.name;
          }
        },
        pickerTitleConfig: BrnPickerTitleConfig(
          cancel: Text(''),
          confirm: Text(''),
          titleContent: '请选择父菜单',
        ),
      ),
    );
  }
}
